package com.wutianyi.images;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GrayImage extends JPanel
{

    /**
     * 
     */
    private static final long serialVersionUID = 6893399709663359436L;

    Image castle;
    BufferedImage bufferedImage;
    Dimension size;
    
    public GrayImage()
    {
        size = new Dimension();
        castle = new ImageIcon(this.getClass().getResource("/banner.jpg")).getImage();
        size.width = castle.getWidth(null);
        size.height = castle.getHeight(null);
        setPreferredSize(size);
        bufferedImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_BYTE_GRAY);
        Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
        g2d.drawImage(castle, 0, 0, null);
        g2d.dispose();
    }
    
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(bufferedImage, 0, 0, null);
    }
    
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Grayscale");
        frame.add(new GrayImage());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
